/**
 * System
 */

import * as cache from '@/util/cache'
import md5 from 'js-md5'
import expired from '@/config/expired'
const versionKey = 'v' + md5('system')

export default {
  namespaced: true,
  state: {
    showTestSystem: false,
    versionInfo: null,
    onlyOfflinePay: false
  },
  getters: {
    showTestSystem: state => state.showTestSystem,
    offlinePay: state => state.onlyOfflinePay,
    versionInfo: state => {
      if (state.versionInfo === null) {
        state.versionInfo = cache.get(versionKey)
      }
      return state.versionInfo
    }
  },
  mutations: {
    SET_VERSION: (state, data) => {
      state.versionInfo = data
      cache.save(versionKey, data, expired.systemExpired)
    },
    SET_TEST_SYSTEM: (state, data) => {
      state.showTestSystem = !!data
    },
    SET_PAY: (state, data) => {
      state.onlyOfflinePay = !!data
    }
  },
  actions: {
    set_version: ({ commit }, data) => {
      commit('SET_VERSION', data)
    },
    set_testSystem: ({ commit }, data) => {
      commit('SET_TEST_SYSTEM', data)
    },
    set_pay: ({ commit }, data) => {
      commit('SET_PAY', data)
    }
  }
}
